package com.bush.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bush.mall.product.entity.dto.AttrGroupDTO;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

import com.bush.mall.common.entity.Result;
import com.bush.mall.common.exception.SaaSException;
import com.bush.mall.product.mapper.AttrGroupMapper;
import com.bush.mall.product.entity.AttrGroupEntity;
import com.bush.mall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * 属性分组
 * @author RUN
 * @date 2022-11-28
 */
@Service(AttrGroupService.BEAN_ID)
@Slf4j
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupMapper, AttrGroupEntity> implements AttrGroupService {

    /**
     * 查询属性分组列表
     * @author RUN
     * @date 2022-12-11
     */
    @Override
    public Result queryForPage(AttrGroupDTO dto) {
        try{
            Page<AttrGroupEntity> page = new Page<>(dto.getCurrent(),dto.getSize());
            LambdaQueryWrapper<AttrGroupEntity> wrapper = new LambdaQueryWrapper<>();
            if (StringUtils.hasText(dto.getAttrGroupName())) wrapper.like(AttrGroupEntity::getAttrGroupName,dto.getAttrGroupName());
            if (Objects.nonNull(dto.getCatelogId())) wrapper.eq(AttrGroupEntity::getCatelogId,dto.getCatelogId());
            baseMapper.selectPage(page,wrapper);
            return Result.ok(page);
        }catch(Exception e){
            log.error("AttrGroupService queryForPage ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 创建属性分组表单对象
     * @author RUN
     * @date 2022-11-28
     */
    @Override
    public Result saveAttrGroup(AttrGroupEntity attrGroup) {
        try{
            Assert.notNull(attrGroup,"参数异常");
            boolean flag = this.save(attrGroup);
            if (flag) return Result.ok("属性分组添加成功");
            throw new SaaSException("属性分组添加失败");
        }catch(Exception e){
            log.error("AttrGroupService saveAttrGroup ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }


    /**
     * 修改属性分组表单对象
     * @author RUN
     * @date 2022-11-28
     */
    @Override
    public Result updateAttrGroup(AttrGroupEntity attrGroup) {
        try{
            Assert.notNull(attrGroup,"参数异常");
            boolean flag = this.updateById(attrGroup);
            if (flag) return Result.ok("属性分组修改成功");
            throw new SaaSException("属性分组修改失败");
        }catch(Exception e){
            log.error("AttrGroupService updateAttrGroup ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }


    /**
     * 通过ids批量删除属性分组
     * @author RUN
     * @date 2022-12-11
     */
    @Override
    public Result deleteAttrGroup(List<Long> ids) {
        try{
            Assert.notNull(ids,"参数异常");
            int count = baseMapper.deleteBatchIds(ids);
            if (count == ids.size()) return Result.ok("通过ids批量删除属性分组");
            throw new SaaSException("通过ids批量删除属性分组");
        }catch(Exception e){
            log.error("AttrGroupService deleteAttrGroup ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 查询通过id
     * @param id id
     * @return {@link Result}
     */
    @Override
    public Result queryById(Long id) {
        try{
            Assert.notNull(id,"参数异常");
            AttrGroupEntity attrGroupEntity = baseMapper.selectById(id);
            return Result.ok(attrGroupEntity);
        }catch(Exception e){
            log.error("AttrGroupService deleteAttrGroup ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }


}